<template>
  <div>
    <!-- 弹出框 -->
    <el-dialog
      :title="title"
      :visible="visible"
      @update:visible="$emit('update:visible', $event)"
      @close="onClosed"
    >
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          label="部门名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="部门编码"
          :label-width="formLabelWidth"
          prop="code"
        >
          <el-input v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="部门负责人"
          :label-width="formLabelWidth"
          prop="manager"
        >
          <!-- focus获得焦点时候触发 -->
          <el-select
            v-model="form.manager"
            @focus="onFocus"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in managers"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="部门介绍"
          :label-width="formLabelWidth"
          prop="introduce"
        >
          <el-input v-model="form.introduce" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('cancel')">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDepartments, editDepartment } from '@/api/departments'

import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    visible: Boolean,
    form: Object
  },
  data () {
    return {
      managers: [],
      formLabelWidth: '120px',

      // 表单校验规则
      rules: {
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '请填写1-50个字符', trigger: 'change' }
        ],
        introduce: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 300,
            message: '请填写1-50个字符',
            trigger: 'change'
          }
        ],
        manager: [{ required: true, message: '不能为空', trigger: 'blur' }],
        name: [
          { required: true, message: '不能为空', trigger: 'blue' },
          { min: 1, max: 50, message: '请填写1-50个字符', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    onClosed () {
      //   this.form = {
      //     introduce: '',
      //     name: '',
      //     code: '',
      //     manager: '',
      //     pid: '' // 父级部门id
      //   }
      this.$emit('cancel')
      // 点击弹框别处关闭要加这个 不然还是有之前的数据
    },
    // 获得焦点触发
    async onFocus () {
      const res = await getEmployeeSimple()
      // console.log(res)
      this.managers = res
    },
    // 提交表单
    submitForm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // alert('成功')
          if (this.form.id) {
            // 有id 调取查看修改接口数据
            const res1 = await editDepartment(this.form)
          } else {
            // 没有走 新增接口数据
            const res = await addDepartments(this.form)
          }
          // 新增成功调用清空数据 不然点开还是原来的数据
          //   this.onCancel()
          this.$emit('cancel')
          // console.log(res)
          this.$message.success(`${this.title}成功`)
          this.dialogFormVisible = false
          //  调用渲染tree数据
          //   this.getData()
          this.$emit('getData')
        } else {
          this.$message.error('数据格式有误')
          return false
        }
      })
    }
  }
}
</script>

<style></style>
